﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSE.Windows.Test
{
    public partial class LingyaoForm : Form
    {
        public Lingyao linyao = null;
        private Yaopin yaopin = null;

        public LingyaoForm()
        {
            InitializeComponent();
        }

        private void LingyaoForm_Load(object sender, EventArgs e)
        {
            if (linyao != null)
            {
                yaopin = linyao.LingyaoYaopin;
                textBoxDrugCode.Text = yaopin.YaopinCode;
                textBoxName.Text = yaopin.YaopinName;
                textBoxType.Text = yaopin.YaopinGuige;
                textBoxUnit.Text = yaopin.YaopinDanwei;
                textBoxNumber.Text = linyao.LingyaoNumber.ToString();
                dateTimePicker1.Value = linyao.LingyaoDate;
                textBoxCode.Text = linyao.LingyaoCode;
                textBoxComment.Text = linyao.LingyaoComment;
            }
        }

        private void textBoxDrugCode_TextChanged(object sender, EventArgs e)
        {
            if (textBoxDrugCode.Text.Length == 3)
            {
                try
                {
                    Yaopin y = AnesthesiaBLL.GetYaopinByCode(textBoxDrugCode.Text);
                    textBoxName.Text = y.YaopinName;
                    textBoxType.Text = y.YaopinGuige;
                    textBoxUnit.Text = y.YaopinDanwei;
                    yaopin = y;
                }
                catch (Exception ex)
                {
                    textBoxName.Text = "";
                    textBoxType.Text = "";
                    textBoxUnit.Text = "";
                    yaopin = null;

                    MessageBox.Show(ex.Message, "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void textBoxDrugCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBoxNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (linyao == null)
                linyao = new Lingyao();

            if (yaopin == null)
            {
                MessageBox.Show("请选择药品！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                textBoxDrugCode.SelectAll();
                textBoxDrugCode.Focus();
                return;
            }

            try
            {
                linyao.LingyaoNumber = Int32.Parse(textBoxNumber.Text);
                linyao.LingyaoDate = DateTime.Parse(dateTimePicker1.Value.ToShortDateString());
                linyao.LingyaoNumber = Int32.Parse(textBoxNumber.Text);
                linyao.LingyaoCode = textBoxCode.Text;
                linyao.LingyaoComment = textBoxComment.Text;
                linyao.LingyaoYaopin = yaopin;

            }
            catch (Exception ex)
            {
                MessageBox.Show("数据格式错误！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
            }
        }

    }
}
